<!-- In this file, I made adjustment to make opening page more proper. In my opinion, working with long horizontal tables is logical idea -->
<!-- For each paragraph and picture, I added  a horizontal table-->
<%@page import="java.util.*,java.text.*"%>
<%@page import="QRCG.DatabaseManager"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
	pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>User Registered Games</title>
<link href="index.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="jquery-1.2.6.min.js"></script>
<script type="text/javascript" src="jquery.accordion.js"></script>
<script type="text/javascript">
	$(document).ready(function() {
		jQuery('#list1b').accordion({
			autoheight : false
		});
		jQuery('#list2b').accordion({
			autoheight : false
		});
		jQuery('#list3b').accordion({
			autoheight : false
		});
	});
</script>
<script language="javascript">
	function popup(url) {
		var win_dow = window.open(url, 'myPopUp',
				"type=fullWindow, fullscreen,scrollbars=yes");
	}
</script>
</head>

<%@ include file="ProfileHeader.jsp"%>

<%
if (session.getAttribute("nickname") != null) {
	ArrayList<HashMap<String, Object>> games = new ArrayList<HashMap<String, Object>>();

	final int DESC_LIMIT = 250; //the char limit of the description shown to the user in the sidebars
	
	String gameinfo = "";
	String url = "";
	String desc = "";
	String winner = "";

	DatabaseManager connection = new DatabaseManager();
	connection.connect();

	String user_id = (String) session.getAttribute("user_id");
	DateFormat readable = new SimpleDateFormat("dd-MM-yyyy");

	DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
	String today = df.format(new Date());

	String bdate, edate; //begin and end dates of the games
	String game_status = "";

	String searched = (String) session.getAttribute("searchedB"); //if no search is done, this value is null
%>

<div id="page">
	<div id="page-bgtop">
		<div id="page-bgbtm">
			<div id="search-content">
				<!-- Write the search query here -->

				<div id=content>


					<%
							games = connection.getUserGames(user_id,0); //limitless

						if (!games.isEmpty()) {
							%>

					<div class="gameinf">
						<div class="entry">
							<p>NAME</p>
						</div>
					</div>

					<div class="gameinf">
						<div class="entry">
							<p>LOCATION</p>
						</div>
					</div>
					<div class="gameinf">
						<div class="entry">
							<p>START DATE</p>
						</div>
					</div>

					<div class="gameinf">
						<div class="entry">
							<p>END DATE</p>
						</div>
					</div>
					
					<div class="gameinf">
						<div class="entry">
							<p>STATUS</p>
						</div>
					</div>
					
					<div class="gameinf">
						<div class="entry">
							<p>WINNER</p>
						</div>
					</div>

					<%for (int i = 0; i < games.size(); i++) {
								HashMap<String, Object> map2 = (HashMap<String, Object>) games
										.get(i);
								bdate = readable.format(map2.get("begin_date"));
								edate = readable.format(map2.get("end_date"));
								
								url = "ViewTheGame.jsp?game_id=" + map2.get("game_id");
								
								winner = "Pending..";
								if(today.compareTo(df.format(map2.get("begin_date")))<0) //the game has not started yet
								{
									game_status = "Pending";
								}
								else if(today.compareTo(df.format(map2.get("end_date")))>0) //the game has finished
								{
									game_status = "Finished";
									winner = connection.get_nicknamefrom_id(connection.gameWinner((Integer)map2.get("game_id")));
									if(winner == null)
										winner = "No winner";
								}
								else //the game is ongoing
									game_status = "Started";
					%>

					<div class="gameinf">
						<div class="entry">
							<a href=<%=url %>> <%=map2.get("name")%>
							</a>
						</div>
					</div>

					<div class="gameinf">
						<div class="entry">
							<p>
								<%=connection.getGameInformation(
							DatabaseManager.GameInformation.LOCATION,
							map2.get("game_id").toString())%>
							</p>
						</div>
					</div>
					<div class="gameinf">
						<div class="entry">
							<p>
								<%=bdate%>
							</p>
						</div>
					</div>

					<div class="gameinf">
						<div class="entry">
							<p>
								<%=edate%>
							</p>
						</div>
					</div>
					
					<div class="gameinf">
						<div class="entry">
							<p>
							<%=game_status %></p>
						</div>
					</div>
					
					<div class="gameinf">
						<div class="entry">
							<p>
								<%=winner%>
							</p>
						</div>
					</div>

					<%
						}
						} 
					%>

				</div>


			</div>
			<!-- end #content -->


			<div id="sidebar">
				<ul>
					<li>
						<div style="clear: both;">&nbsp;</div>
					</li>
					<li>
						<h2>ALL GAMES</h2>
					</li>

					<li>
						<div class="basic" id="list1b">

							<%
								games = connection.getAllGames();

								if (!games.isEmpty()) {
									for (int i = 0; i < games.size(); i++) {
										HashMap<String, Object> map1 = (HashMap<String, Object>) games
												.get(i);

										url = "ViewTheGame.jsp?game_id=" + map1.get("game_id");

										gameinfo = "Location: "
												+ connection.getGameInformation(
														DatabaseManager.GameInformation.LOCATION,
														map1.get("game_id").toString());

										desc = (String) map1.get("description");

										if (desc.length() > DESC_LIMIT) {
											desc = desc.substring(0, DESC_LIMIT);
											desc += "...";
										}

										gameinfo += "<br>Description: " + desc;
							%>
							<a><%=map1.get("name")%></a>
							<div>
								<p><%=gameinfo%></p>
								<p style="cursor: pointer; color: #D14836;"
									onclick="javascript:popup('<%=url%>')">Click here for more
									information</p>
							</div>

							<%
								}
								}
							%>
						</div>
					</li>
				</ul>
			</div>
			<!-- end #sidebar -->


			<div style="clear: both;">&nbsp;</div>
		</div>
	</div>
</div>
<!-- end #page -->

<%
	} else {
		response.sendRedirect("index.jsp");
	}
%>
<%@ include file="footer.jsp"%>